package com.hyzh.latte.vehicle.service;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.map.MapUtil;
import com.baomidou.dynamic.datasource.annotation.DS;
import com.coffee.core.CoffeeException;
import com.hyzh.latte.vehicle.dmo.entity.*;
import com.hyzh.latte.vehicle.nezha.PoleEdge;
import com.hyzh.latte.vehicle.nezha.PoleVertex;
import com.hyzh.latte.vehicle.repository.*;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
 * @author songkui
 * @since 2024/3/20 17:13
 */
@Service
@DS("latte")
@Slf4j
@AllArgsConstructor
public class NezhaService {

    private final PolePGRepository polePGRepository;
    private final PolePointPGRepository polePointPGRepository;
    private final CameraPGRepository cameraPGRepository;
    private final ParkPGRepository parkPGRepository;
    private final GatePGRepository gatePGRepository;
    private final VtypePGRepository vtypePGRepository;

    //查询所有
    public Map<String, List<PoleVertex>> mapPoleVertex(){
       List<PolePGEntity>  list = polePGRepository.list();
       if (CollectionUtil.isEmpty(list)){
           return null;
       }
       return list.stream().collect(Collectors.groupingBy( PolePGEntity::getParkCode,
               Collectors.mapping(PolePGEntity::buildVertex , Collectors.toList())));
    }


    public Map<String, List<PoleEdge>>  mapPoleEdg(){
        List<PolePointPGEntity> list = polePointPGRepository.list();
        if (CollectionUtil.isEmpty(list)){
            return null;
        }

        return list.stream().collect(Collectors.groupingBy( PolePointPGEntity::getParkCode,
                Collectors.mapping(  PolePointPGEntity::buildEdge,   Collectors.toList()  ) ));
    }

    public Map<String, Map<String, CameraPGEntity>> mapCamera(){
        List<CameraPGEntity> list = cameraPGRepository.list();
        if (CollectionUtil.isEmpty(list)){
            return null;
        }

        return list.stream().collect(Collectors.groupingBy(CameraPGEntity::getParkCode,
                Collectors.toMap(CameraPGEntity::getCameraCode, Function.identity()) ));
    }

    public  List<String> parkList(){
        List<ParkPGEntity> list = parkPGRepository.list();
        if (CollectionUtil.isEmpty(list)){
            return null;
        }

//        return list.stream().collect(Coll ectors.toMap(ParkPGEntity::getParkCode, ParkPGEntity::getParkCode));
        return list.stream().map(ParkPGEntity::getParkCode).collect(Collectors.toList());
    }

    public Map<String, ParkPGEntity> mapPark(){
        List<ParkPGEntity> list = parkPGRepository.list();
        if (CollectionUtil.isEmpty(list)){
            return null;
        }
        for (ParkPGEntity pgEntity : list){
            if (pgEntity.checkGather()){
                throw new CoffeeException("Gather config is null");
            }
        }
        return list.stream().collect(Collectors.toMap(ParkPGEntity::getParkCode, Function.identity()));
    }


//    public Map<String, PointPGEntity> pointMap(){
//        List<PointPGEntity> list = pointPGRepository.list();
//        if (CollectionUtil.isEmpty(list)){
//            return null;
//        }
//        return list.stream().collect(Collectors.toMap(PointPGEntity::getPointNo, Function.identity()));
//    }

    //key: park;  v: key: gateNo;
    public Map<String, Map<String,GatePGEntity>> gateMap(){
        List<GatePGEntity> list = gatePGRepository.list();
        if (CollectionUtil.isEmpty(list)){
            return null;
        }
        List<GatePGEntity> pList = new ArrayList<>();
        for (GatePGEntity gatePGEntity : list){
            if (gatePGEntity.getGateNo().contains(",")){
                String[] ns = gatePGEntity.getGateNo().split(",");
                for (String n : ns){
                    pList.add(gatePGEntity.copyEntity(n));
                }
            }else {
                pList.add(gatePGEntity);
            }
        }

        return pList.stream().collect(Collectors.groupingBy(GatePGEntity::getParkCode,
                Collectors.toMap(GatePGEntity::getGateNo, Function.identity()) ));
    }



    public Map<String, Map<String, PolePGEntity>> poleEntityMap(){
        List<PolePGEntity> list = polePGRepository.list();
        if (CollectionUtil.isEmpty(list)){
            return null;
        }
        return list.stream().collect(Collectors.groupingBy(PolePGEntity::getParkCode,
                Collectors.toMap(PolePGEntity::getPoleCode, Function.identity()) ));

    }

    //key vno,  value: type
    public Map<String, String> vtype(){
        List<VtypeEntity> list = vtypePGRepository.list();
        if (CollectionUtil.isEmpty(list)){
            return null;
        }
        Map<String, String> map = MapUtil.newHashMap(list.size());
        for (VtypeEntity entity : list){
            map.put(entity.getVno(), entity.getType());
        }
        return map;
    }


}
